﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_MyPartySite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();

        int id = getPartyId();
        var party = db.Parties.SingleOrDefault(x => x.PartyId.Equals(id));

        SiteHeader.Text = party.Tema;
        LabDate.Text = "  " + party.Date;
        LabTime.Text = "  " + party.Time;

        if (party.Food == true)
        {
            LabFood.Text = "ja";
        }
        else
        {
            LabFood.Text = "nej";
        }

        if (party.Drink == true)
        {
            LabDrink.Text = "ja";
        }
        else
        {
            LabDrink.Text = "nej";
        }

        LabComment.Text = party.Comment;

        var participent = from pp in db.PartyParticipants
                          where pp.PartyId == party.PartyId
                          join u in db.aspnet_Users on pp.UserId equals u.UserId
                          select u;
        LWParticipatingPeople.DataSource = participent;
        LWParticipatingPeople.DataBind();

        var applicants = from n in db.Notifications
                         where n.PartyId == party.PartyId
                         join u in db.aspnet_Users on n.UserId equals u.UserId
                         select new { u.PictureDir, u.Name, u.LastName, n.NotificationsId };

        LWApplicants.DataSource = applicants;
        LWApplicants.DataBind();
    }

    public int getPartyId()
    {
        return Convert.ToInt32(Request.QueryString["PartyId"]);
    }

    protected void BtnRequestParticipation_Click(object sender, EventArgs e)
    {
        string PartyId = Request.QueryString["partyid"];
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        var userid = db.aspnet_Users.SingleOrDefault(x => x.UserId.Equals(Membership.GetUser().ProviderUserKey));
        Notification notification = new Notification();
        notification.Message = "Ansøgning";
        notification.IsRead = false;
        notification.PartyId = Convert.ToInt32(PartyId);
        notification.UserId = userid.UserId;
        db.Notifications.InsertOnSubmit(notification);
        db.SubmitChanges();
        RequestStatus.Text = "Ansøgning sendt";
        Response.Redirect(Request.RawUrl);
    }

    protected void BtnAccept_Click(object sender, EventArgs e)
    {
        Button myButton = (Button)sender;
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        var noti = db.Notifications.Single(x => x.NotificationsId.Equals(myButton.CommandArgument));
        PartyParticipant pp = new PartyParticipant();
        pp.PartyId = noti.PartyId;
        pp.UserId = noti.UserId;
        db.PartyParticipants.InsertOnSubmit(pp);
        db.Notifications.DeleteOnSubmit(noti);
        db.SubmitChanges();
        Response.Redirect(Request.RawUrl);
    }
    protected void BtnDeny_Click(object sender, EventArgs e)
    {
        Button myButton = (Button)sender;
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        var noti = db.Notifications.Single(x => x.NotificationsId.Equals(myButton.CommandArgument));
        db.Notifications.DeleteOnSubmit(noti);
        db.SubmitChanges();
        Response.Redirect(Request.RawUrl);
    }
}